/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package schoolbank;

/**
 *
 * @author meydlo
 */
public class Account {
    
    private double balance;
    private String accountID;
    private String accountType;
    
    private String[] accountTypes = new String[]{"salary","children","business"};
    
    public Account(String accountID, String accountType) throws Exception
    {
        if(setAccountType(accountType) < 0)
            throw new Exception("allowed account types: "+arrayStoS(this.getAccountTypes()));
        this.accountID = accountID;
        this.balance = 0;
    }
                                         
    public double withdraw(double amount)
    {
      double returnVal = -1;         // to not return "balance" 
      double newBalance = 0;        // value of testing for correctness

      if (amount <= Double.MAX_VALUE && amount >= 0)
        {
          newBalance = balance - amount;
          if (newBalance >= 0 && newBalance <= Double.MAX_VALUE)
          {
            returnVal = newBalance;
            balance = newBalance;
          }
        }
        return returnVal;
        
    }
    
    public double deposit(double amount)
    {
      double returnVal = -1;         // to not return "balance" 
      double newBalance = 0;        // value of testing for correctness

      if (amount <= Double.MAX_VALUE && amount >= 0)
        {
          newBalance = balance + amount;
          if (newBalance >= 0 && newBalance <= Double.MAX_VALUE)
          {
            returnVal = newBalance;
            balance = newBalance;
          }
        }
      
        return returnVal;
    }

  
    public double calculateAnnualInterest(double adding, double interest, double fee)
    {
        double annualInterest = 0;
            switch(accountType)
            {
                case "salary":
                    annualInterest = balance*(interest+adding);
                    if (interest+adding>0.1)
                        fee = fee*1.5;
                    balance = balance + annualInterest-fee;
                    break;
                case "children":
                    annualInterest = balance*interest;
                    balance = balance + annualInterest;
                    break;
                case "business":
                    annualInterest = balance*(interest+adding);
                    balance = balance - annualInterest;
                    break;
            }
        return annualInterest;
    }
  
    /***************************************************************
     * EXTRA
     **************************************************************/
    
    public String arrayStoS(String[] arrayS)
    {
        String returnS = "";
        for(String s : arrayS){
            returnS = returnS+s+",";
        }
        returnS = returnS.substring(0, returnS.length()-1);
        return returnS;
    }
    /***************************************************************
     * GETTERS / SETTERS
     **************************************************************/
    
    public double getBalance()
    {
        return this.balance;
    }
    
    public String[] getAccountTypes()
    {
        return accountTypes;
    }
    
    public int setAccountType(String accountType)
    {
        boolean found = false;
        int i = 0;
        while(!found && i < accountTypes.length)
        {
            if(accountTypes[i].equals(accountType))
                found = true;
            i++;
        }
        
        if(found){
            this.accountType = accountType;
            return 0;
        }
        else{
            return -1;
        }
    }
}
